/** index.js By Beginner Emain:zheng_jinfan@126.com HomePage:http://www.zhengjinfan.cn */

layui_navbar(navs);

$('.admin-header-menu li a').on('click',function(){
    var id = $(this).data('id');
    $.post(url, {id:id}, function (res) {
        if (res.status === 1) {
           layui_navbar(res.sideMenus,'again');
        }
    }, 'json');
    
})

function layui_navbar(navs,type){
    var tab;
    var elem;
    if(type!='again'){
     elem = '.admin-nav-card';
    }
    layui.config({
        base: ADMIN+'/js/',
        version: new Date().getTime()
    }).use(['element', 'layer', 'navbar', 'tab'], function () {
        var element = layui.element(),
        $ = layui.jquery,
        layer = layui.layer,
        navbar = layui.navbar(),
        tab = layui.tab({
            elem: elem, //设置选项卡容器
            /*maxSetting: {
                max: 5,
                tipMsg: '最多同时只能打开5个选项！'
            },*/
            contextMenu: true
            /*onSwitch: function (data) {
                console.log(data.id); //当前Tab的Id
                console.log(data.index); //得到当前Tab的所在下标
                console.log(data.elem); //得到当前的Tab大容器

                console.log(tab.getCurrentTabId())
            }*/
        });
        //iframe自适应
        $(window).on('resize', function () {

            var $content = $('.admin-nav-card .layui-tab-content');
            $content.height($(this).height() - 147);
            $content.find('iframe').each(function () {
                $(this).height($content.height());
            });
        }).resize();

        //设置navbar
        navbar.set({
            spreadOne: true,
            elem: '#admin-navbar-side',
            cached: true,
            data: navs
            /*cached:true,
            url: 'datas/nav.json'*/
        });
        //渲染navbar
        navbar.render();
        //监听点击事件
        
        navbar.on('click(side)', function (data) {
            tab.tabAdd(data.field);
        });
        //清除缓存
        $('#clearCached').on('click', function () {
            navbar.cleanCached();
            layer.alert('清除完成!', { icon: 1, title: '系统提示' }, function () {
                location.reload();//刷新
            });
        });

        $('.admin-side-toggle').on('click', function () {
            var sideWidth = $('#admin-side').width();
            if (sideWidth === 200) {
                $('#admin-body').animate({
                    left: '0'
                }); //admin-footer
                $('#admin-footer').animate({
                    left: '0'
                });
                $('#admin-side').animate({
                    width: '0'
                });
            } else {
                $('#admin-body').animate({
                    left: '200px'
                });
                $('#admin-footer').animate({
                    left: '200px'
                });
                $('#admin-side').animate({
                    width: '200px'
                });
            }
        });
        $('.admin-side-full').on('click', function () {
            var docElm = document.documentElement;
            //W3C  
            if (docElm.requestFullscreen) {
                docElm.requestFullscreen();
            }
            //FireFox  
            else if (docElm.mozRequestFullScreen) {
                docElm.mozRequestFullScreen();
            }
            //Chrome等  
            else if (docElm.webkitRequestFullScreen) {
                docElm.webkitRequestFullScreen();
            }
            //IE11
            else if (elem.msRequestFullscreen) {
                elem.msRequestFullscreen();
            }
            layer.msg('按Esc即可退出全屏');
        });

        $('#setting').on('click', function () {
            tab.tabAdd({
                href: '/Manage/Account/Setting/',
                icon: 'fa-gear',
                title: '设置'
            });
        });
        //手机设备的简单适配
        var treeMobile = $('.site-tree-mobile'),
            shadeMobile = $('.site-mobile-shade');
        treeMobile.on('click', function () {
            $('body').addClass('site-mobile');
        });
        shadeMobile.on('click', function () {
            $('body').removeClass('site-mobile');
        });
    });
    var isShowLock = false;
}